package com.example.myapplication;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class Myadapter2 extends RecyclerView.Adapter<Myadapter2.Viewholder> {
    private List<Album> list1;

    // 传入值
    public Myadapter2(List list) {
        list1=list;
    }

    @NonNull
    @Override
    public Viewholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        // 压缩器（from生成静态压缩器）
        // 闪退时可以挂布尔值false运行
        View view=LayoutInflater.from(parent.getContext()).inflate(R.layout.item1,parent,false);
        Viewholder viewholder=new Viewholder(view);
        return viewholder;
    }

    @Override
    public void onBindViewHolder(@NonNull Viewholder holder, int position) {
        Album album=list1.get(position);
        // 获取专辑的名称和封面图片
        holder.textView.setText(album.getName());
        holder.imageView.setImageResource(album.getImageid());
    }

    @Override
    public int getItemCount() {
        return list1.size();
    }

    // 内嵌类 holder类似一个行指针
    static class Viewholder extends RecyclerView.ViewHolder{
        View view;
        // 存放专辑名称
        TextView textView;
        // 存放专辑封面
        ImageView imageView;

        public Viewholder(@NonNull View itemView) {
            super(itemView);
            view=itemView;
            // item1.xml中的控件对象
            textView=itemView.findViewById(R.id.textView4);
            imageView=itemView.findViewById(R.id.imageView);
        }
    }
}
